import User from "./User"
import AxiosMethods from "@/util/AxiosMethods";
const myuser=User.state.user.userid;

const state = {
    myProjects:[],
}
const mutations = {
    // 替换当前项目列表
    replaceMyProjects(state,projects){
        state.myProjects = projects
        // 为每一个项目列表对象的photo属性前面添加地址字段
        state.myProjects.forEach(project => {
            project.photo ='src/assets/images/'+ project.photo
        })
    },
}
const getters = {
    myProjects: state => state.myProjects,
}
const actions = {
    // 获取当前用户的项目列表
    async selectProject({commit},id){
        try {
            const parms={createUid:id}
            const response = await AxiosMethods.post('/project/select',parms)
            if(response.data.code === 1){
                // console.log("这里是uid="+id+"的项目列表：",response.data.data)
                commit('replaceMyProjects',response.data.data)
            }
        } catch (error) {
            console.log(error)
        }
    },
    // 删除项目,ids为项目pid组成的数组
    async deleteProjects({commit},ids){
        try {
            const response=await AxiosMethods.delete(`/project/${ids}`,{headers:{'Content-Type':'application/x-www-form-urlencoded'}})
            if(response.data.code === 1){
                console.log("删除成功")
            }
        }catch (error) {
            console.log(error)
        }
    },
    //更新项目信息
    async updateProject({commit},project){
        // console.log("更新项目信息：",project)
        try {
            const response=await AxiosMethods.put('/project',project)
            if(response.data.code === 1){
                // console.log("更新成功")
            }
        }catch (error) {
            console.log(error)
        }
    }
}

export default {
    namespaced: true,
  state,
  getters,
  mutations,
  actions
}